#include "alicefilesystemmodel.h"
#include "utils.h"

#include <QDateTime>

AliceFileSystemModel::AliceFileSystemModel(QObject *parent) :
    QFileSystemModel(parent)
{
}

QVariant AliceFileSystemModel::data ( const QModelIndex & index, int role) const{

    if (role == Qt::DisplayRole){
        QFileInfo fileInfo = this->fileInfo(index);
        QString note;
        QString place;
        QDateTime time;
        this->getDescription(fileInfo, time, note, place);
        QString label;
        QString toolTip;
        this->generateLabel(fileInfo,label,toolTip,time, note, place);
        return  QVariant(label);
    } else {
        return QFileSystemModel::data(index, role);
    }

}


void AliceFileSystemModel::getDescription ( const QFileInfo & fileInfo, QDateTime &time, QString &note, QString &place  ) const
{
    QSet<QString> *keywords = new QSet<QString>();

    QFileInfo *descFile;
    QString dane = "";
    QDir commentDir ( fileInfo.dir() );
    QString commentString;
    if ( commentDir.cd ( ".comments" ) )
    {
        descFile = new QFileInfo ( commentDir.absoluteFilePath (fileInfo.fileName() ) +".xml" );
        if ( descFile->exists() && !fileInfo.isDir())
        {
            if ( Utils::unGzip ( descFile, &dane ) )
            {
                commentString = dane;
                //Wyciaganie komentarza
                QString tekst = dane.section ("<Note>",1,1);
                tekst = tekst.section("</Note>", 0,0);
                note = tekst;

                //Wyciaganie miejsca
                tekst = dane.section("<Place>",1,1);
                tekst = tekst.section("</Place>",0,0);
                place = tekst;

                //WyciÃÂganie czasu
                tekst = dane.section("<Time>",1,1);
                tekst = tekst.section("</Time>",0,0);
                if (tekst != "")
                {
                    time.setTime_t(tekst.toUInt());
                }

                //Wyciaganie miejsca
                tekst = dane.section("<Keywords>",1,1);
                tekst = tekst.section("</Keywords>",0,0);
                QStringList lista = tekst.split(",", QString::SkipEmptyParts);
                QList<QString>::const_iterator i;
                for (i=lista.constBegin(); i != lista.constEnd(); i++)
                {
                    keywords->insert(*i);
                }


            }
            else
            {
                commentString.clear();
            }
        }
        else
        {
            commentString.clear();
        }
        delete descFile;
    }
    else
    {
        dane = fileInfo.fileName();
        commentString.clear();
    }
}

/**
* Generuje Opis i ToolTipa z danymi zdjÃÂcia
*/
void AliceFileSystemModel::generateLabel(QFileInfo &file, QString &label, QString &toolTip, QDateTime time, QString note, QString place ) const
{

    QString opis;
    if (file.isDir())
    {
        if (file.fileName() == "..")
        {
            opis = "..(Up)";
        }
        else
        {
            opis =file.fileName();

        }

    }
    else
    {
        toolTip = QObject::tr("Picture data: ");
        toolTip.append("\n" + QObject::tr("File Name: ") + file.fileName());
        if (note != "")
        {
            toolTip.append("\n" + QObject::tr("Comment: ") + note);
        }
        if (place != "")
        {
            toolTip.append("\n" + QObject::tr("Place: ") + place);
            opis.append(this->cutString(place));
        }
        if (time.isValid() && time.toTime_t()!=0)
        {
            toolTip.append("\n" + QObject::tr("Time: ") + time.toString());
            opis.append("\n" + time.date().toString());
            opis.append("\n" + time.time().toString());

        }
    }
    if (opis == "")
    {
        opis = this->cutString(file.fileName());
        label = opis;

    }
    else
    {
        label = opis;


    }

}


QString AliceFileSystemModel::cutString(QString string) const
{
    string = string.left(30);
    if (string.size() == 30) string.append("...");
    if (string.size()>17)
    {
        if (string.lastIndexOf(" ", 18)>0) return string.replace(string.lastIndexOf(" ", 18), 1, "\n");
        string.chop((20 - string.size())*(-1));
        string = string.append("...");

    }
    return string;

}
